package _interview100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 49. 字母异位词分组
 */
public class No49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> hashMap = new HashMap<>();
        for (String str : strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String key = new String(charArray);
            List<String> arrayList = hashMap.getOrDefault(key, new ArrayList<>());
            arrayList.add(str);
            hashMap.put(key, arrayList);
        }
        return new ArrayList<>(hashMap.values());
    }
}
